package com.caopeng.javaee.controller;

import com.caopeng.javaee.dao.CourseJpa;
import com.caopeng.javaee.entity.course;
import com.caopeng.javaee.service.TeacherService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.sql.Timestamp;
import java.util.*;
/**
 * @program: javaee
 * @description: 教师的业务处理放这里
 * @author: caopeng
 * @create: 2018-12-07 22:31
 **/
@Controller
@RequestMapping(value = "teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherservice;
    @Autowired
    CourseJpa coursejpa;
    //跳转添加课程界面
    @RequestMapping(value = "To_add_course")
    public String To_add_course(HttpSession session,Model model){
        System.out.println("进入跳转界面");
        int i=(int)session.getAttribute("user_id");
        String name=teacherservice.TfindById(i).getName();
        System.out.println("教师名字"+name);
        model.addAttribute("name",name);
        return "T_add_course";
    }
    @PostMapping(value = "TAddCourse")
    public String up(MultipartHttpServletRequest request, HttpServletRequest hrequest, HttpSession session,Model model) throws IOException {
        //System.out.println(session.getAttribute("user_id"));
        String course_name=hrequest.getParameter("course_name");
        String course_teacher=hrequest.getParameter("course_teacher");
        String course_introduce=hrequest.getParameter("course_introduce");
        Iterator<String> fileNames = request.getFileNames();
        String originalFilename="";
        while (fileNames.hasNext()) {
            String fileName = fileNames.next();
            System.out.println("filename:" + fileName);
            List<MultipartFile> fileList = request.getFiles(fileName);
            if (fileList.size() > 0) {
                Iterator<MultipartFile> fileIte = fileList.iterator();
                while (fileIte.hasNext()) {
                    MultipartFile multipartFile = fileIte.next();
                    originalFilename = multipartFile.getOriginalFilename();
                    //System.out.println("文件名字"+originalFilename);
                    String path = "D:\\j2ee\\javaee\\src\\main\\resources\\static\\uploadfile\\";
                    File dir = new File(path);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    String filePath = path + originalFilename;
                    File dest = new File(filePath);
                    if (!(dest.exists())) {
                        multipartFile.transferTo(dest);
                    }
                }
            }
        }
        //System.out.println(course_name+" "+ course_teacher+" "+course_introduce);
        Timestamp create_time=new Timestamp(new Date().getTime());
        coursejpa.save(new course(course_name,String.valueOf(session.getAttribute("user_id")),course_introduce,originalFilename,create_time,"1"));
       List<course> allcourse=coursejpa.findByCourseTeacherId(String.valueOf(session.getAttribute("user_id")));
       System.out.println("allcourse的数量"+allcourse.size());
        model.addAttribute("user_name",course_teacher);
        model.addAttribute("teacher_class",allcourse);
        return "teacher_index";
    }
}

